<template>
  <div>
    <el-row type="flex" justify="end">
      <el-col :span="14">
        <!-- 下拉菜单 -->
        <el-select v-model="currentYear" style="width: 120px" size="small" @change="dateChange">
          <el-option v-for="(item, index) in yearList" :key="index" :label="item" :value="item">
            {{ item }}
          </el-option>
        </el-select>

        <el-select v-model="currentMonth" style="width: 120px;margin-left:10px" size="small" @change="dateChange">
          <el-option v-for="item in 12" :key="item" :label="item" :value="item">
            {{ item }}
          </el-option>
        </el-select>
      </el-col>

    </el-row>

    <!-- 日历内容 -->
    <el-calendar v-model="currentDate">
      <template v-slot:dateCell="{date,data}">
        <div class="date-content">
          <span class="text">{{ data.day | getDay }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>

  </div>
</template>

<script>
export default {
  filters: {
    getDay(value) {
    //   value.split('-') 是数组 索引为2是字符串
      const day = value.split('-')[2]
      //   判断开始是否是0 是的话截取第一个个字符
      return day.startsWith('0') ? day.substr(1) : day
    }
  },
  props: {
    startDate: {
      // 这里默认必须是函数形式返回
      type: Date,
      default: () => new Date()
    }
  },
  data() {
    return {
      currentYear: null,
      currentMonth: null,
      currentDate: null,
      yearList: []
    }
  },
  created() {
    this.currentMonth = this.startDate.getMonth() + 1
    this.currentYear = this.startDate.getFullYear()
    this.yearList = Array.from(Array(11), (value, index) => index + this.currentYear - 5)
    // 钩子函数执行 必须调用一次
    this.dateChange()
  },

  methods: {

    // 是否是休息日
    isWeek(value) {
      return value.getDay() === 6 || value.getDay() === 0
    },
    // 年月份改变之后
    dateChange() {
      const year = this.currentYear
      const month = this.currentMonth
      // 当前的日期 以当前的月份的1号开始
      this.currentDate = new Date(`${year}-${month}-1`)
    }
  }
}
</script>

<style  scoped>
 div /deep/ .el-calendar-day {
  height:  auto;
 }
 div /deep/ .el-calendar-table__row td,div /deep/ .el-calendar-table tr td:first-child,  div /deep/ .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  position: relative;
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  position: absolute;
  right: 0;
  top: 0;
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}
.el-backtop, .el-calendar-table td.is-today .text {
   display: inline-block;
   width: 20px;
   height: 20px;
   background: #409eff;
   color: #fff;
   border-radius: 50%;
}

 div /deep/ .el-calendar-table td.is-selected .text{
   background: red;
   color: #fff;
   border-radius: 50%;
 }
 div /deep/ .el-calendar__header {
   display: none
 }
</style>

